<template>
	<view class="logon">
		<view class="title">欢迎注册</view>
		<u-form :model="form" ref="uForm">
			<u-form-item>
				<u-input  class="inp" v-model="form.username" placeholder="请输入用户名" :border="form.border" :border-color="form.colore" />
			</u-form-item>
			<u-form-item>
				<u-input type="password" class="inp" v-model="form.password" placeholder="请输入密码" :password-icon="form.passwordIcon" :border="form.border" :border-color="form.colore" />
				
				</u-verification-code>
			</u-form-item>
<!-- 			<u-form-item label="性别">
			        <u-input v-model="form.sex" type="select" @click="showSex = true" />
			</u-form-item> -->
			<u-form-item>
				<u-input class="inp" v-model="form.phone" placeholder="请输入手机号" :border="form.border" :border-color="form.colore" />
			</u-form-item>
			<u-form-item>
				<u-input v-model="form.code" placeholder="请输入验证码" :border="form.border" :border-color="form.colore" />
				<u-button :disabled="form.disabled" slot="right" size="mini" @click="getCode">{{tips}}</u-button>
			</u-form-item>
		</u-form>
				<u-radio-group v-model="value" @change="radioGroupChange">
					<u-radio 
						@change="radioChange" 
						v-for="(item, index) in list" :key="index" 
						:name="item.name"
						:disabled="item.disabled"
						checked='true'
					>
						{{item.name}}
					</u-radio>
				</u-radio-group>
		<view class="button-login">
			<u-button  type="success" @click="Logon" shape="circle"  >注册</u-button>
		</view>

		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form:{
					username:'',
					password:'',
					phone:'',
					code:'',
					rCode:'',
					border:true,
					colore:'#FAFAFA',
					passwordIcon: true,
					disabled:false,
					num:60,
					sex:'男'
				},
				tips: '验证码',
				value: '男',
				list: [
								{
									name: '男',
									disabled: false
								},
								{
									name: '女',
									disabled: false
								},
							],
			}
		},
		methods: {
			Logon(){
				if(this.form.code == ''){
					this.$u.toast('请输入验证码');
					return
				}
				else if(this.form.rCode !== this.form.code){
					this.$u.toast('验证码错误');
					return
				}
				console.log(this.form.code,this.form.rCode);
				
				uniCloud.callFunction({
					name:"unicloud",
					data:{
						username:this.form.username,
						password:this.form.password,
						phone:this.form.phone,
						sex:this.form.sex
					}
				}).then(res=>{
					console.log(res)
					this.$u.toast("登录成功")
				})
				
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode(){
				let regex = /^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$\%\^\&\*\(\)])[0-9a-zA-Z!@#$\%\^\&\*\(\)]{8,16}/; 
				let phoneNumber = /^[1][3-9][0-9]{9}$/;
				if(this.form.password == ''){
					this.$u.toast('请输入密码');
					return
				}else if(!regex.test(this.form.password)){
					this.$u.toast('密码必含有8-16位的英文数字特殊符号')
					return
				}else if(this.form.phone == ''){
					this.$u.toast('请输入手机号');
					return
				}else if(!phoneNumber.test(this.form.phone)){
					this.$u.toast('请输入正确的手机号');
					return
				}else{
					this.form.disabled = true
					var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K','L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f','g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
					var Code = "";
					for (var i = 0; i < 4; i++) {
					             var randCode = Math.floor(Math.random() * (arr.length - 1)); 
					                Code += arr[randCode];
					}
					this.form.rCode = Code;
					this.$u.toast(Code);
					let that=this
					var num = 60;
					let time = setInterval(countDown, 1000);
					function countDown(){
						
					
						that.tips = num;
						num--
						if( num < 0 ){
							clearInterval(time);
							that.form.disabled = false
							that.tips = "验证码"
						}
						
					}
					return
					
				}
				
			},
			
			radioChange(e){
				// this.form.sex = e
			},
			radioGroupChange(e) {
					this.form.sex = e
					console.log(this.form.sex)
			}

		}
	}
</script>

<style lang="scss" scoped>
.title{
	width: 200rpx;
	font-size: 50rpx;
	font-weight: 600;
	margin: 20rpx auto;
}
.inp{
	margin: 20rpx auto;
}

</style>
